package com.arceus.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : iron
 * @version : 1.0.0
 * @date : 下午4:19 2018/9/28
 * 54 螺旋矩阵
 */

public class SpiralMatrix {


    public static List<Integer> s(int[][] matrix) {
        List<Integer> res = new ArrayList<>();
        if (matrix == null || matrix.length == 0) {
            return res;
        }
        int numi = matrix.length;
        int numj = matrix[0].length;
        //环数
        int loop = numi > numj ? (numj + 1) / 2 : (numi + 1) / 2;

        for (int i = 0; i < loop; i++, numi -= 2, numj -= 2) {

            for (int col = i; col < i + numj; col++) {
                res.add(matrix[i][col]);
            }
            for (int row = i + 1; row < i + numi; row++) {
                res.add(matrix[row][i + numj - 1]);
            }
            //最后一环为一行或一列，则在上面两步之后已经全部输出，直接退出即可
            if (numi == 1 || numj == 1)
                break;

            for (int col = i + numj - 2; col >= i; col--) {
                res.add(matrix[i + numi - 1][col]);
            }
            for (int row = i + numi - 2; row > i; row--) {
                res.add(matrix[row][i]);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int arr[][] = {
                {1, 2, 4, 5, 5},
                {2, 3, 4, 6, 5},
                {3, 33, 6, 7, 5}};
        List<Integer> res = s(arr);
        for (Integer a : res) {
            System.out.println(a);
        }
    }
}
